/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili.server.db.tasks;

import java.util.ArrayList;
import java.util.Arrays;
import pl.cezary.butler.walili.common.Task;
import java.util.List;
import pl.cezary.butler.walili.common.TaskStatus;
import pl.cezary.butler.walili.server.config.ServerConfiguration;
import pl.cezary.butler.walili.server.db.Queries;
import pl.cezary.butler.walili.server.db.utils.TableDefinitionUtils;

/**
 *
 * @author Przemysław Bałdyga
 */
//Do przemyślenia nie działa na razie
public class GetTasksByStatusTask extends FetchingTask<List<Task>> {

    private static final int MAX_PRIORITY = ServerConfiguration.getInstance().getMaxPriority();
    private final Integer[] STATUS;
    private List<Task> wynik;

    public GetTasksByStatusTask(TaskStatus taskStatus) {
        this(taskStatus.ordinal());
    }

    public GetTasksByStatusTask(TaskStatus... taskStatus) {
        this.STATUS = new Integer[taskStatus.length+1];
        for (int i = 0; i < taskStatus.length; ++i) {
            this.STATUS[i] = taskStatus[i].ordinal();
        }
        this.STATUS[taskStatus.length]=MAX_PRIORITY;
    }

    public GetTasksByStatusTask(int STATUS) {
        this.STATUS = new Integer[]{STATUS, MAX_PRIORITY};
    }

   
    @Override
    public void executeInternal() { // TODO zerknij na implementacje pobierania zadan wg zapytania       
        wynik = (List) getJdbcTemplate().query(
                Queries.SELECT_ZADANIE.getQuery() + " WHERE status IN(" + TableDefinitionUtils.generateParameters(STATUS.length-1) + ") AND priorytet <= ?",
                STATUS,
                TaskRowMapper.getInstance());
    }

    @Override
    protected List<Task> $getResultsNonLocking() {
        return wynik;
    }
}
